const { address } = require("./address");
const { itemlist } = require("./itemlist");



function pdd(){

    var bid = applist[1]
    f.active(bid, 5)

    // 打开拼多多
    f.ms({"text":"多多买菜"}, true, 2)

    f.ms({"textMatches":"当前自提点是.*"}, false, 2)

    var shop_ = textMatches("当前自提点是.*").findOne(200)
    
    if ( shop_ ){
        log(shop_)
        let match = shop_.text().match(/当前自提点是(.*?)，点击进行自提点切换/);
        if (match && match[1]) {
            console.log(match[1]); // 输出结果: "大碗面米线店"
            var shop = match[1]
        } else {
            console.log("No match found");
        }
    }

    f.ms({"text":"点击进入搜索功能"}, true, 2)

    f.ms({"text":"搜索发现"}, false, 2)

    var itemlist = item.itemlist()
    setText(0, itemlist[appinfo.item])
    sleep(1000)
    f.ms({"text":itemlist[appinfo.item], "className":"android.widget.TextView"}, true, 2)

    f.ms({"textMatches":".*点击加入购物车"}, false, 2)


    const titleList = textMatches(/.*点击加入购物车/).find();
    if ( titleList ){
        for (var i = 0;i<titleList.length;i++){
            var d = titleList[i];
            if ( d.text().includes(itemlist[appinfo.item]) ){
                let match = d.text().match(/(\d+\.\d)(?=斤|千克)/);

                if (match) {
                    // 将匹配到的字符串转换为数字
                    let num = parseFloat(match[0]);
                    // 四舍五入到小数点后两位
                    var weight = num.toFixed(2);
                    console.log("重量:", weight); // 输出结果: "4.90"
                } else {
                    console.log("No match found");
                }

                let yun = d.text().match(/(\d+\.\d+)元/);
                if ( yun ){
                    // 将匹配到的字符串转换为数字
                    let num = parseFloat(yun[0]);
                    // 四舍五入到小数点后两位
                    var yun_num = num.toFixed(2);
                    log('价钱:', yun_num)
                }

                var price = (yun_num/weight).toFixed(2)
                log("price", price)

                var address_List = addressList.address()

                var arr = {
                    "product":itemlist[appinfo.item],
                    "city":address_List[appinfo.address],
                    "price":price,
                    "pdate":appinfo.date,
                    'mall':'拼多多',
                    'shop': shop,
                    'code':'',
                    'spot': spot_list[appinfo.address],
                }
                var post_data = {
                    s:'App.Index.Update_where',                    
                    table:'jilingcaiji',
                    arr:JSON.stringify(arr),
                    where:JSON.stringify(arr)
                }

                post_.post_wenfree(post_data)
                post_.post_ybl(arr)

                back();
                sleep(1000)
                back();
                sleep(1000)

                appinfo.item++
                f.setLocal('item', appinfo.item)
                break;
            }
        }
    }
}


module.exports = {
    pdd
};






